package com.tobe.spring_demo.lifecycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * @author yangqifan 2015年9月16日
 * 
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor , InitializingBean{

	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println(beanName + " :bean post processor run before..");
		return bean;
		//the bean instance to use, either the original or a wrapped one; if null, no subsequent BeanPostProcessors will be invoked
		//动态代理可以返回一个包装的类,没创建一个bean都会调用这个方法
		//BeanPostProcessor也是一个bean,spring会先扫描然后在创建bean,这样子才可以每创建一个bean,都调用该方法
	}

	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("bean post processor run after..");
		return bean;
	}

	public void afterPropertiesSet() throws Exception {
		System.out.println("after property set...");
		
	}
	
	/**
	 *  especially when using BeanFactoryPostProcessors and BeanPostProcessors. 
	 *  These mechanisms implement important functionality such as property placeholder replacement and AOP.
	 *  
	 *  property placeholder replacement 是一个BeanFactoryPostProcessor, 创建bean后可以修改bean definition的定义,把${},替换掉
	 *  
	 *  AOP 使用BeanPostProcessor在创建bean后,创建一个bean的代理并返回
	 */

}

